home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Control del ratón / PoePoem / PoePoem.cs next >
Encoding:
Text File  |  2002-04-18  |  5.8 KB  |  131 lines

  1. //--------------------------------------
  2. // PoePoem.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class PoePoem: Form
  9. {
  10.      const string strAnnabelLee =
  11.  
  12.           "It was many and many a year ago,\n"                         +
  13.           "   In a kingdom by the sea,\n"                              +
  14.           "That a maiden there lived whom you may know\n"              +
  15.           "   By the name of Annabel Lee;\x2014\n"                     +
  16.           "And this maiden she lived with no other thought\n"          +
  17.           "   Than to love and be loved by me.\n"                      +
  18.           "\n"                                                         +
  19.           "I was a child and she was a child\n"                        +
  20.           "   In this kingdom by the sea,\n"                           +
  21.           "But we loved with a love that was more than love\x2014\n"   +
  22.           "   I and my Annabel Lee\x2014\n"                            +
  23.           "With a love that the wingΘd seraphs of Heaven\n"            +
  24.           "   Coveted her and me.\n"                                   +
  25.           "\n"                                                         +
  26.           "And this was the reason that, long ago,\n"                  +
  27.           "   In this kingdom by the sea,\n"                           +
  28.           "A wind blew out of a cloud, chilling\n"                     +
  29.           "   My beautiful Annabel Lee;\n"                             +
  30.           "So that her highborn kinsmen came\n"                        +
  31.           "   And bore her away from me,\n"                            +
  32.           "To shut her up in a sepulchre,\n"                           +
  33.           "   In this kingdom by the sea.\n"                           +
  34.           "\n"                                                         +
  35.           "The angels, not half so happy in Heaven,\n"                 +
  36.           "   Went envying her and me\x2014\n"                         +
  37.           "Yes! that was the reason (as all men know,\n"               +
  38.           "   In this kingdom by the sea)\n"                           +
  39.           "That the wind came out of the cloud by night,\n"            +
  40.           "   Chilling and killing my Annabel Lee.\n"                  +
  41.           "\n"                                                         +
  42.           "But our love it was stronger by far than the love\n"        +
  43.           "   Of those who were older than we\x2014\n"                 +
  44.           "   Of many far wiser than we\x2014\n"                       +
  45.           "And neither the angels in Heaven above\n"                   +
  46.           "   Nor the demons down under the sea\n"                     +
  47.           "Can ever dissever my soul from the soul\n"                  +
  48.           "   Of the beautiful Annabel Lee:\x2014\n"                   +
  49.           "\n"                                                         +
  50.           "For the moon never beams, without bringing me dreams\n"     +
  51.           "   Of the beautiful Annabel Lee;\n"                         +
  52.           "And the stars never rise, but I feel the bright eyes\n"     +
  53.           "   Of the beautiful Annabel Lee:\x2014\n"                   +
  54.           "And so, all the night-tide, I lie down by the side\n"       +
  55.           "Of my darling\x2014my darling\x2014my life and my bride,\n" +
  56.           "   In her sepulchre there by the sea\x2014\n"               +
  57.           "   In her tomb by the sounding sea.\n"                      +
  58.           "\n"                                                         +
  59.           "                                       [May 1849]\n";
  60.  
  61.      readonly int   iTextLines = 0;
  62.      int            iClientLines, iStartLine = 0;
  63.      float          cyText;
  64.  
  65.      public static void Main()
  66.      {
  67.                // Ver si tiene sentido ejecutar el programa.
  68.  
  69.           if (!SystemInformation.MouseWheelPresent)
  70.           {
  71.                MessageBox.Show("íEl programa necesita un rat≤n con rueda!",
  72.                                "Poema de Poe", MessageBoxButtons.OK,
  73.                                MessageBoxIcon.Error);
  74.                return;
  75.           }
  76.                // En caso contrario, seguir normalmente.
  77.  
  78.           Application.Run(new PoePoem());
  79.      }
  80.      public PoePoem()
  81.      {
  82.           Text = "\"Annabel Lee\", de Edgar Allan Poe";
  83.           BackColor = SystemColors.Window;
  84.           ForeColor = SystemColors.WindowText;
  85.           ResizeRedraw = true;
  86.  
  87.                // Calcular el n·mero de lφneas del texto.
  88.  
  89.           int iIndex = 0;
  90.  
  91.           while((iIndex = strAnnabelLee.IndexOf('\n', iIndex)) != -1)
  92.           {
  93.                iTextLines++;
  94.                iIndex++;
  95.           }
  96.                 // Obtener el valor del espacio de lφnea.
  97.  
  98.          Graphics grfx = CreateGraphics();
  99.          cyText = Font.GetHeight(grfx);
  100.          grfx.Dispose();
  101.  
  102.          OnResize(EventArgs.Empty);
  103.      }
  104.      protected override void OnResize(EventArgs ea)
  105.      {
  106.           base.OnResize(ea);
  107.  
  108.           iClientLines = (int) (ClientSize.Height / cyText);
  109.  
  110.           iStartLine = Math.Max(0, 
  111.                        Math.Min(iStartLine, iTextLines - iClientLines));
  112.      }
  113.      protected override void OnMouseWheel(MouseEventArgs mea)
  114.      {
  115.           int iScroll = 
  116.                mea.Delta * SystemInformation.MouseWheelScrollLines / 120;
  117.  
  118.           iStartLine -= iScroll;
  119.           iStartLine  = Math.Max(0, 
  120.                         Math.Min(iStartLine, iTextLines - iClientLines));
  121.           Invalidate();
  122.      }
  123.      protected override void OnPaint(PaintEventArgs pea)
  124.      {
  125.           Graphics grfx = pea.Graphics;
  126.  
  127.           grfx.DrawString(strAnnabelLee, Font, new SolidBrush(ForeColor),
  128.                           0, -iStartLine * cyText);
  129.      }
  130. }
  131.